package com.lianniu.web.tea.base.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.tea.base.service.TeaBaseService;

@Controller
@RequestMapping("/rest/tea/base")
public class TeaRestBaseController {
	
	private final static Logger logger = Logger.getLogger(TeaRestBaseController.class);

	@Resource
	private TeaBaseService teaBaseService;
	
	@RequestMapping(value = "/getTeaProductList", method = RequestMethod.POST )
	public @ResponseBody BaseResult getTeaProductList(HttpServletRequest req){
		String areaCode = req.getParameter("areaCode");
		logger.info("TeaBaseController  getTeaProductList begin.");
		try {
			logger.info("getTeaProductList begin.");
			BaseResult baseResult = teaBaseService.getForestryList(areaCode);
			logger.info("getTeaProductList end.");
			logger.info("TeaBaseController getTeaProductList end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaBaseController getTeaProductList error:" + e.getMessage());
			logger.error("TeaBaseController getTeaProductList end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value = "/uploadImage", method = RequestMethod.GET )
	public @ResponseBody BaseResult uploadImage(HttpServletRequest req){
		String mediaId = req.getParameter("mediaId");
		String component = req.getParameter("component");
		logger.info("TeaBaseController  uploadImage begin.");
		try {
			logger.info("uploadImage begin.");
			BaseResult baseResult = teaBaseService.uploadImage(mediaId,component);
			logger.info("uploadImage end.");
			logger.info("TeaBaseController uploadImage end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaBaseController uploadImage error:" + e.getMessage());
			logger.error("TeaBaseController uploadImage end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value = "/getUserMsg", method = RequestMethod.GET )
	public @ResponseBody BaseResult getUserMsg(HttpServletRequest req){
		String openid = req.getParameter("openid");
		String accessToken = req.getParameter("accessToken");
		logger.info("TeaBaseController  getUserMsg begin.");
		try {
			logger.info("getUserMsg begin.");
			String url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
			url = url.replace("OPENID", openid);
			url = url.replace("ACCESS_TOKEN", accessToken);
			//创建默认的httpClient实例.   
	        CloseableHttpClient httpclient = null;  
	        //接收响应结果  
	        CloseableHttpResponse response = null;  
	        String resStr = "";
	        try {  
	            //创建httppost  
	            httpclient = HttpClients.createDefault();    
	            HttpGet httpGet = new HttpGet(url);
	            response = httpclient.execute(httpGet);  
	            //解析返结果  
	            HttpEntity entity = response.getEntity();   
	            if(entity != null){  
	                resStr = EntityUtils.toString(entity, "UTF-8");      
	                System.out.println(resStr);  
	            }  
	        }catch (Exception e) {
	        		e.printStackTrace();
			}
			logger.info("getUserMsg end.");
			logger.info("TeaBaseController getUserMsg end.");
			return BaseResult.success(resStr);
		} catch (Exception e) {
			logger.error("TeaBaseController getUserMsg error:" + e.getMessage());
			logger.error("TeaBaseController getUserMsg end.");
			return BaseResult.exception(e.getMessage());
		}
	}
}
